package com.fsc.comadapoc1.application
{
	import com.fsc.comadapoc1.domain.PortfolioModel;
	import com.fsc.comadapoc1.domain.UserModel;
	import com.fsc.comadapoc1.entities.LiquidityReportRequest;
	import com.fsc.comadapoc1.events.LoadLiquidityReportChartEvent;
	import com.fsc.comadapoc1.events.LoadLiquidityReportEvent;
	
	import flash.events.EventDispatcher;
	
	import mx.controls.Alert;
	import mx.controls.DateField;
	import mx.rpc.AsyncToken;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.remoting.RemoteObject;
	import mx.utils.ArrayUtil;

	

	[Event(name="liquidityReportChartComplete",type="com.fsc.comadapoc1.events.LoadLiquidityReportChartEvent")]
	[ManagedEvents(names="liquidityReportChartComplete")]
	public class GetLiquidityReportChartCommand extends EventDispatcher
	{
		public function GetLiquidityReportChartCommand()
		{
		}
		
		

		[Inject(id="portfolioService")]
		public var service:RemoteObject;
		
		/**
		 * Contains objects to manage portfolios
		 */ 
		[Inject]
		[Bindable]
		public var model:PortfolioModel;
		
		
		[Inject]
		[Bindable]
		public var userModel:UserModel;
		
		
		private var inited:Boolean = false;
		
		public function init():void{
			trace("Initializing Get portfolio funds Command");
			
			inited = true;
		}
		
		
		public function execute(event:LoadLiquidityReportChartEvent):AsyncToken {
			if(!inited){
				init();
			}
			var liquidReportReqParams:LiquidityReportRequest = event.liquidityReportRequest;
			return service.LiquidityGraph( userModel.guid,
											model.currentPortfolio.id,
											liquidReportReqParams.asAt,
											liquidReportReqParams.referenceCurrency,
											liquidReportReqParams.exitFee
										) as AsyncToken;
		}
		
		/******************************************
		 *  Result handler 
		 */
		public function result(data:Object):void
		{
			var searchCompleteEvent:LoadLiquidityReportChartEvent = new LoadLiquidityReportChartEvent(LoadLiquidityReportChartEvent.LOAD_LIQUIDITY_REPORT_CHART_COMPLETE);
			if ( data.Results != null)
			{
		
				var arr:Array;
				var arrCleaned:Array = new Array();
				arr = ArrayUtil.toArray(data.Results)
				for each (var obj:Object in arr )
				{
					if(obj.LadderDate != null){
						arrCleaned.push(obj);
					}
				}
					
					
					
				//arrCleaned.forEach(forEach_DateFilter);
				model.currentPortfolio.liquidityReportChart.source = arrCleaned;
				
				
				trace("Load liq report results");
			}
			else
			{
				trace("Load liq report: Clearing Results");
				
			}
			dispatchEvent(searchCompleteEvent);
			}
		
		/******************************************
		 *  Fault handler in case the request failed. 
		 */
		public function error (fault:FaultEvent, trigger:LoadLiquidityReportChartEvent) : void {
			//need to add some logging here
			Alert.show(fault.fault.message,fault.fault.faultDetail);
		}  
		
		private function forEach_DateFilter(element:*, index:int, arr:Array):void {
			
			if(element.LadderDate !=null && element.LadderProceeds){
				var dt:Date = new Date( element.LadderDate );
				
				//ajwt this was working 15/08 element.LadderDate = DateField.dateToString(dt,"DD/MM/YYYY");
				trace(dt);
				trace(DateField.dateToString(dt,"DD/MM/YYYY"));
				
			
				//arr.splice(index,5);
			}
				
	
		}
		
		
	}
}